package com.neuedu.javase.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.collection 
 * date:   2025/11/5   10:26  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyHashMap {


    public static void main(String[] args) {

        //这两个 字符串 有hash碰撞
        System.out.println("\"FB\".hashCode() = " + "FB".hashCode());
        System.out.println("\"Ea\".hashCode() = " + "Ea".hashCode());

        /**
         * 问题： 当使用纯数组的时候 查找元素  需要挨个遍历，
         */
        // int len = 9999999;
        // Human[] humans = new Human[len];
        // for (int i = 0; i < len; i==) {
        //     humans[i] = new Human("张三"+i, 18+i);
        // }
        //
        // System.out.println("humans = " + humans);
        //
        //
        // for (int i = 0; i < humans.length; i==) {
        //     Human human = humans[i];
        //     System.out.println("查找到 第 "+i+" 个元素");
        //     if("张三9999999".equals(human.getName())){
        //         System.out.println("张三7的年龄 = " + human.getAge());
        //         break;
        //     }
        // }


        /**
         * 解决问题
         *
         *  Hash        Key          Value
         *  24021607    张三0       new Human("张三0"， 18);
         *  24021608    张三1       new Human("张三1"， 19);
         *  24021609    张三2       new Human("张三2"， 20);
         *  24021610    张三3       new Human("张三3"， 21);
         *  24021611    张三4       new Human("张三4"， 22);
         *
         *  "FB".hashCode() = 2236
         *  "Ea".hashCode() = 2236
         *
         */



        // for (int i = 0; i <10 ; i==) {
        //     System.out.println("(\"张三\"+i) = " + ("张三" + i));
        //     System.out.println(("张三" + i).hashCode());
        // }


        /**
         * 创建对象
         */
        // HashMap map = new HashMap();
        // 指定容器大小
        HashMap map = new HashMap(100);



        //添加元素
        map.put("张三", new Human("张三", 18));
        map.put("李四", new Human("李四", 28));
        map.put("王五", new Human("王五", 38));
        map.put("赵六", new Human("赵六", 48));
        map.put("FB", new Human("FB", 58));
        map.put("Ea", new Human("Ea", 68));
        // map.put("zhaoqi ", 100);


        //map 获取元素
        Object lisi = map.get("李四");
        Human human = (Human) lisi;
        System.out.println("human.getAge() = " + human.getAge());


        System.out.println("======================== putAll ==============================================+");
        Map map2 = new HashMap();
        System.out.println("map = " + map);
        System.out.println("map2 = " + map2);

        //Map2 中有值
        map2.put("赵六", new Human("赵六", 98));
        map2.putAll(map);  //重复的key  保留新的value
        System.out.println("putAll map2 = " + map2);

        //get 当key 不存在 返回null 不会报错
        System.out.println("======================== get ==============================================+");

        System.out.println("map = " + map);
        System.out.println("map.get(\"王五\") = " + map.get("王五")); //Human{name='王五', age=38}}
        System.out.println("map.get(\"xxxx\") = " + map.get("xxxx"));  //null



        System.out.println("======================== remove ==============================================+");

        Map  person1 = new HashMap();
        person1.put("name","张飞");
        // person1.put("age",Integer.valueOf(50));
        person1.put("age",50);
        person1.put("address","黑龙江HRBU");

        System.out.println("person1 = " + person1);

        System.out.println("person1.remove(\"address\") = " + person1.remove("address"));
        System.out.println("person1 = " + person1);

        System.out.println("======================== 判断是否存在 key  value  ==============================================+");

        System.out.println("person1.containsKey(\"name\") = " + person1.containsKey("name"));
        System.out.println("person1.containsValue(\"张飞\") = " + person1.containsValue("张飞"));

        System.out.println("======================== 遍历  KeySet  stu.keySet()     ==============================================+");


        Map  stu = new HashMap();
        stu.put("id","2300001");
        stu.put("name","刘备");
        stu.put("age",50);
        stu.put("address","黑龙江HRBU");

        //得到所有的key
        Set keys = stu.keySet();
        //通过遍历key  找到  keys
        for (Object key : keys) {   //或者是  keys.iterator()
            Object value = stu.get(key);
            System.out.printf("key:   %s \t value:%s\r\n" , key.toString() ,value.toString());
        }

        System.out.println("======================== 遍历   stu.values()     ==============================================+");

        //遍历values  Collection -->  java.util.HashMap$Values
        Collection values = stu.values();
        System.out.println("values.getClass() = " + values.getClass());
        for (Object value : values) {
            System.out.printf(" value:%s\r\n" , value.toString());
        }


        System.out.println("======================== 遍历    stu.entrySet();     ==============================================+");
        Set<Map.Entry<Object, Object>> nodeSet = stu.entrySet();
        //遍历节点
        for (Map.Entry entry : nodeSet) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.printf("key:   %s \t value:%s\r\n" , key.toString() ,value.toString());
        }


    }




}
